package com.dc.seckill.controller;

import com.dc.seckill.domain.MiaoshaUser;
import com.dc.seckill.redis.RedisService;
import com.dc.seckill.redis.UserKey;
import com.dc.seckill.service.MiaoshaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dc.seckill.domain.User;
import com.dc.seckill.result.CodeMsg;
import com.dc.seckill.result.Result;
import com.dc.seckill.service.UserService;

@Controller
@RequestMapping("/demo")
public class DemoController {
	
	@Autowired
	UserService userService;

    @Autowired
    RedisService redisService;

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }
    
    //
    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello() {
    	return Result.success("hello,daijs");
    }
    
    @RequestMapping("/helloError")
    @ResponseBody
    public Result<String> helloError() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }
    
    @RequestMapping("/view")
    @ResponseBody
    public String view(Model model) {
    	model.addAttribute("name","daijs");
        return "hello";
    }
    
    @RequestMapping("/db/dbget")
    @ResponseBody
    public Result<User> dbGet(){
    	User user = userService.getById(1);
    	return Result.success(user);
    }
   
    @RequestMapping("/db/dbinsert")
    @ResponseBody
    public Result<Boolean> dbinsert(){
    	Boolean b = userService.saveUser();
    	return Result.success(b);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public  Result<User> redisGet(){
        User user = redisService.get(UserKey.getById,""+1,User.class);
        return Result.success(user);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet(){
        User user = new User();
        user.setId(1);
        user.setName("daijs");
        boolean b1 = redisService.set(UserKey.getById,""+1,user);
        return Result.success(b1);
    }

    @RequestMapping("/saveuser")
    @ResponseBody
    public Result<Boolean> saveUser(){
        MiaoshaUser user = miaoshaUserService.getById(18010670875L);
        for(int i=1 ;i<=1000;i++){
            MiaoshaUser miaoshaUser = new MiaoshaUser();
            miaoshaUser.setId((long) i);
            miaoshaUser.setNickname(user.getNickname()+i);
            miaoshaUser.setPassword(user.getPassword());
            miaoshaUser.setSalt(user.getSalt());
            miaoshaUser.setRegisterDate(user.getRegisterDate());
            miaoshaUser.setLoginCount(1);
            miaoshaUser.setLastLoginDate(user.getLastLoginDate());
            userService.saveUser(miaoshaUser);
        }
        return Result.success(true);
    }
}
	
